﻿package  com.devilcat.ws{
	import com.devilcat.ws.Basicws;
	import com.worlize.websocket.*;
	import com.devilcat.dt.Gamedt;
	import com.devilcat.dt.Pokemondt;
	
	public class UserPokemonws extends Basicws{
		var mUid:int;
		public var loadOne:Boolean = false;
		public function UserPokemonws(uid:int) {
			super("iw-protocol");
			mUid=uid;
		}
		override function handleWebSocketOpen(event:WebSocketEvent):void
		{
			var pack:Object = new Object();
			pack.uid=mUid;
			pack.action="loaduserpokemon";
			var myJson:String = JSONString(pack);
			websocket.sendUTF(myJson);
		}
		override function handleWebSocketMessage(event:WebSocketEvent):void
		{
			if (event.message.type === WebSocketMessage.TYPE_UTF8)
			{	
				var i:int;
				var pokemonList:Array = new Array();
				pokemonList =	JSONObject(event.message.utf8Data);
				Gamedt.Player.pokemonList = new Array();
				for (i=0; i<pokemonList.length;i++){
					Gamedt.Player.pokemonList[i]=new Pokemondt();
					Gamedt.Player.pokemonList[i].setPersonal(pokemonList[i]);
				}
				
				for (i=0; i<Gamedt.Player.pokemonList.length; i++){
					if(Gamedt.Player.pokemonList[i].upid==Gamedt.Player.leadpkm){
						var temp:Pokemondt =Gamedt.Player.pokemonList[i];
						Gamedt.Player.pokemonList[i]=Gamedt.Player.pokemonList[0];
						Gamedt.Player.pokemonList[0]=temp;
					}
				}
				
				Gamedt.MainUI.preLoadtxt.text="Load Item";
				loadUserItemList();
				//debug(event.message.utf8Data);
			}
			closeConnect();
		}
		function loadUserItemList(){
			if(!loadOne)
			var lupl:UserItemws = new UserItemws(Gamedt.Player.uid);			
		}

	}
	
}
